/*
 * Copyright (c) 2023 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#ifndef __NETWORK_ADAPTER_LOG_COLLECTION_H__
#define __NETWORK_ADAPTER_LOG_COLLECTION_H__

#include <mc/core/timer.h>
#include <mc/engine/service.h>
#include <atomic>

#include <device_tree/interface/NetworkAdapter/LogCollection.h>

#include <ncsi_over_mctp/ncsi_over_mctp_huawei.h>
#include <smbus/smbus.h>

namespace dev {

using ncsi_over_mctp_hw_ptr = std::shared_ptr<ncsi_over_mctp_huawei>;
using smbus_obj_ptr = mc::shared_ptr<smbus>;

class NetworkAdapter_LogCollection
    : public mc::engine::interface<NetworkAdapter_LogCollection,
                                   gen::NetworkAdapter_LogCollection> {
public:
    NetworkAdapter_LogCollection()  = default;
    ~NetworkAdapter_LogCollection() = default;
    bool DumpErrorLog(const std::string& file_path) override;
    bool DumpLastWordLog(const std::string& file_path) override;
    bool DumpRunningLog(const std::string& file_path) override;
    bool DumpBlackBoxLog(const std::string& file_path) override;
    bool CollectLogByNcsi(const std::string& log_dir) override;
    bool CollectLogBySmbus(const std::string& log_dir) override;

    void init_ncsi_endpoint(ncsi_over_mctp_hw_ptr ncsi_endpoint);
    void init_smbus_obj(smbus_obj_ptr smbus_obj);
    void clean_ncsi_endpoint();
    void clean_smbus_obj();

private:
    bool get_log_by_smbus(uint16_t opcode, const std::string& log_path);
    
    ncsi_over_mctp_hw_ptr m_ncsi_over_mctp_huawei;
    smbus_obj_ptr m_smbus_obj;
    std::atomic<bool> m_is_log_collecting_by_smbus{false}; // 标记是否正在通过SMBus收集日志
    std::atomic<bool> m_is_log_collecting_by_ncsi{false};  // 标记是否正在通过NCSI收集日志
};

} // namespace dev

#endif // NETWORK_ADAPTER_LOG_COLLECTION_H